package com.jang.utils;

import com.alibaba.fastjson.JSON;
import com.jang.Entity.cases.CaseRuningEntity;
import com.jang.Entity.cases.ConfigEntity;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.Test;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpUtil {


    public static String Post(CaseRuningEntity caseRuningEntity){
        if(caseRuningEntity.getBody().isEmpty()){
            return "body 为空";
        }else if (caseRuningEntity.getUrl().isEmpty()){
            return "url 为空";
        }
        RequestSpecification httpRequest = RestAssured.given();
        List<ConfigEntity> list = caseRuningEntity.getConfig_list();
        for (int i=0;i<list.size();i++){
            System.out.println(list.get(i).getHeader_key()+"==="+list.get(i).getHeader_value());
            httpRequest.header(list.get(i).getHeader_key(),list.get(i).getHeader_value());
        }
        System.out.println(caseRuningEntity.getUrl());
        System.out.println(caseRuningEntity.getBody());
        final Response response= httpRequest.contentType(ContentType.JSON).body(caseRuningEntity.getBody()).post(caseRuningEntity.getUrl());
        return response.body().asString();
    }

    public String   test(){
        Map<String,String> map=new HashMap();
        map.put("Content-Type","application/json");
        map.put("timestamp","1590567673299");
        map.put("platform","web");
        map.put("token","");
        String url = "http://18.16.200.117:8008/api-proxy/web/user/login";
        String body="{\"loginName\": \"jiangj\",\"password\": \"111111\"}";
        //String result=Post(body,url,map);
        // String s = ResultHandle.handle("token",result,1);
        //   System.out.println(result);
        return "result";
    }

    @Test
    public void  tes(){
        Map<String,String> map=new HashMap();
        map.put("Content-Type","application/json");
        map.put("timestamp","1590567673299");
        map.put("platform","web");
        map.put("token","");
        // String result = test();
        //map=ResultHandle.handerReplace(map,result);
        String url = "http://18.16.200.117:8008/api-proxy/web/preloan/productList";
        String body="{}";
        //  String token =result;
        //String res=Post(body,url,map);
    }


}
